﻿using System;
using System.Data.Linq;
using System.Data.SqlClient;

namespace MvePublicService
{
	/*
	 * 日本SGI株式会社
	 * ヨハンソン・ヘンリク
	 * johansson@sgi.co.jp
	 * 
	 * Comments:
	 * 
	 * Change Log:
	 * ---------------------------------------------------------------------------
	 * 2009.01.07	Johansson Henrik		Class Created.
	 * 2009.01.16							DataContext methods removed,
	 *										changed catch clauses to catch 
	 *										only relevant exceptions.
	 *										
	 */

	/// <summary>
	/// This static class contains methods for obtaining and closing
	/// connections to the Database.
	/// Use this class to open and close database connections and data contexts.
	/// </summary>
	/// <remarks>INSERT:Creds</remarks>
	internal static class DBConnectionHandler
	{
		/// <summary>
		/// Gets an Open connection to the database using the specified
		/// connectionString.
		/// </summary>
		/// <param name="connectionString">The connectionString to use for the connection.</param>
		/// <returns>An Open SqlConnection connection to the Database.</returns>
		internal static SqlConnection OpenDBConnection(string connectionString)
		{
			try
			{
				SqlConnection connection = new SqlConnection(connectionString);
				connection.Open();

				return connection;
			}
			catch (InvalidOperationException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				throw;
			}
			catch (SqlException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				throw;
			}
		}//end:OpenDBConnection

		/// <summary>
		/// Closes and Disposes a Database connection.
		/// Always use this method to close your Database connections.
		/// </summary>
		/// <param name="connection">The SqlConnection to Close.</param>
		internal static void CloseDBConnection(SqlConnection connection)
		{
			try
			{
				connection.Close();
				connection.Dispose();
			}
			catch (SqlException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				throw;
			}
		}//end:CloseDBConnection
	}
}
